import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
/*整合例子1*/

class Product {
  final String name;

  // Product(this.name);
  const Product({required this.name});
}

typedef void CartChangeCallback(Product product, bool inCart);

class ShoppingListItemDemo extends StatelessWidget {
  final Product product;
  final bool inCart;
  final CartChangeCallback onCartChanged;
  ShoppingListItemDemo({required Product product,
    required this.inCart, required this.onCartChanged})
      : product = product,
        super(key: new ObjectKey(product));


  Color _getColor(BuildContext context){
    return inCart?Colors.black54:Theme.of(context).primaryColor;
  }

  TextStyle? _getTextStyle(BuildContext context){
    if(!inCart){
      return null;
    }
    return TextStyle(color: Colors.black54,
      decoration: TextDecoration.lineThrough,
    );
  }



  @override
  Widget build(BuildContext context) {
    return ListTile(
      onTap: () {
        onCartChanged(product, !inCart);
      },
      //??product.name[0]
      leading: CircleAvatar(
        backgroundColor:_getColor(context),
        child: Text(product.name[0]),
      ),
      title: Text(product.name,style: _getTextStyle(context)),
    );
  }

}